package com.rs.admin.controller.sy;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rs.admin.common.RespResult;
import com.rs.admin.mapper.UserInfoMapper;
import com.rs.admin.module.UserInfo;
import com.rs.admin.module.ao.LoginAO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Slf4j
@Api(tags = "登录")
@RestController
public class LoginController {

    @Resource
    UserInfoMapper userInfoMapper;


    @ApiOperation("登录")
    @PostMapping("/login")
    public RespResult login(@RequestBody LoginAO login) {

        if (StringUtils.isEmpty(login.getName())) {
            return RespResult.buildFail("用户名或者密码错误");
        }

        if (StringUtils.isEmpty(login.getPassword())) {
            return RespResult.buildFail("用户名或者密码错误");
        }

        LambdaQueryWrapper<UserInfo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserInfo::getName, login.getName());
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        if (null == userInfo) {
            return RespResult.buildFail("用户名或者密码错误");
        }

        String pw = DigestUtils.md5Hex(login.getPassword() + login.getPassword());

        if (userInfo.getPassword().equals(pw)) {
            return RespResult.buildSuccess(pw);
        }

        return RespResult.buildFail("用户名或者密码错误");
    }


}
